//
//  CZQHomeViewController.m
//  LoveBeer
//
//  Created by 崔正强 on 17/4/26.
//  Copyright © 2017年 崔正强. All rights reserved.
//

#import "CZQHomeViewController.h"
#import "CZQUnLoginAddressView.h"
#import "CZQLoginManager.h"
#import "CZQButton.h"
#import "CZQScanViewController.h"
#import "CZQCatergoryTableViewCell.h"
#import "CZQADTableViewCell.h"
#import "CZQNaviTitleView.h"




@interface CZQHomeViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>

@property (weak,nonatomic) UITableView* tableView;

@property (weak,nonatomic)  CZQNaviTitleView* naviTitleView;

@end
static NSString* Catergory_cell = @"Catergory_cell";
static NSString* AD_cell = @"AD_cell";


@implementation CZQHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self setNaviBar];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    if (!_LoginMgr.isLogin) {
        
        [self setUnLoginAddressView];
        
        return;
    } else {
        
        [self setupUI];
    }
    
    self.navigationController.delegate = self;
}

- (void)setUnLoginAddressView {
    
    CZQUnLoginAddressView *unLoginView = [[CZQUnLoginAddressView alloc] initWithFrame:self.view.bounds];
    
    [self.view addSubview:unLoginView];
    
    unLoginView.viewController = self;
}

#pragma - 设置界面 -
- (void)setupUI {
    UITableView* tabelView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    
    self.tableView = tabelView;
    
    [self.view addSubview:tabelView];
    
    tabelView.delegate = self;
    tabelView.dataSource = self;
    tabelView.translatesAutoresizingMaskIntoConstraints = YES;
    tabelView.estimatedRowHeight = 300;
//    tabelView.rowHeight = 300;
    [tabelView registerClass:[CZQCatergoryTableViewCell class] forCellReuseIdentifier:Catergory_cell];
    [tabelView registerNib:[UINib nibWithNibName:@"CZQCatergoryTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:Catergory_cell];
    [tabelView registerClass:[CZQADTableViewCell class] forCellReuseIdentifier:AD_cell];
    
    tabelView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
    
    [self addBtns];
}

#pragma - 添加导航栏按键 -
- (void)addBtns{
    
    CZQNaviTitleView* coverView = [[CZQNaviTitleView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    self.naviTitleView = coverView;

    [self.view insertSubview:coverView aboveSubview:self.tableView];
    self.view.backgroundColor = [UIColor colorWithRed:254/255.0 green:213/255.0 blue:48/255.0 alpha:0.0];

}
#pragma mark - 数据源方法 -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0){
        return 1;
    }else {
        return 5;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        CZQADTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:AD_cell forIndexPath:indexPath];
        [cell setCategoryDidClickBlock:^(ADCategoryType type) {
            
        }];
        cell.backgroundColor = [UIColor whiteColor];

        return cell;
    }else{
        CZQCatergoryTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:Catergory_cell forIndexPath:indexPath];
        cell.backgroundColor = [UIColor randomColor];
        [cell setCatergorySelectedBlock:^{
            self.tabBarController.selectedIndex = 1;
        }];
        return cell;
    }
    
    
}

#pragma - 代理方法 -
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}



#pragma mark - 点击事件 -
//扫一扫
- (void)actionScan:(id)sender{
    
//    [self.navigationController pushViewController:[CZQScanViewController new] animated:YES];

}

//搜索
- (void)actionSearch:(id)sender{
    
}

#pragma mark - UINavigationControllerDelegate代理方法 -

- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 如果进入的是当前视图控制器
    if (viewController == self) {
        
        self.navigationController.navigationBar.hidden = YES;
    } else {
        // 进入其他视图控制器
        self.navigationController.navigationBar.alpha = 1;
        // 背景颜色设置为系统默认颜色
        self.navigationController.navigationBar.tintColor = nil;
        self.navigationController.navigationBar.translucent = NO;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.y < 99.0 ){
        CGFloat insert = (scrollView.contentOffset.y - 35.0);
        //小于0保持透明
        if (insert < 0 ) insert = 0;

        [self.naviTitleView setTintalpha:insert/64.0];
    }else{
        [self.naviTitleView setTintalpha:1.0];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1){
        return 360;
    }else{
        return 290;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    //去除点击效果
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    //去除选中效果
    cell.selected = NO;

}

@end
